#include "snakel.h"
#include "ui_snakel.h"
#include<qrect.h>
#include<qtimer.h>
#include<QKeyEvent>
#include<QDebug>
#include<QFont>
#include<QPixmap>
#include <QRandomGenerator>

Snakel::Snakel(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Snakel),blsRun(false),speed(500)
{
    ui->setupUi(this);
    this->setGeometry(QRect(600,300,290,310));
}

Snakel::~Snakel()
{
    delete ui;
}
void Snakel::paintEvent(QPaintEvent *event){//update()或程序第一次启动时自动执行，用于画图
    QPainter painter(this);
    if(!blsRun){
    InitSnakel();
    }
    //画游戏背景
    //外墙
    painter.setPen(Qt::black);//设置画笔颜色
    painter.setBrush(Qt::gray);//设置填充颜色
    painter.drawRect(15,15,260,260);
    painter.setPen(Qt::black);//设置画笔颜色
       painter.setBrush(Qt::black);//设置填充颜色
       painter.drawRect(20,20,250,250); //画矩形
       painter.drawPixmap(20,20,250,250,QPixmap(":/images/bc.jpg"));
       /*painter.setPen(Qt::blue);//设置画笔颜色
          for(int i=2;i<=27;i++){
              painter.drawLine(20,i*10,270,i*10);//画横线，270是根据坐标换算出来的
              painter.drawLine(i*10,20,i*10,270);
          }*/
          QFont font1("Courier",24);
              painter.setFont(font1);
              painter.setPen(Qt::red);
              painter.setBrush(Qt::red);
              painter.drawText(40,150,sDisplay);
              //画蛇
              QFont font2("Courier",24);
              painter.setFont(font2);
              painter.setPen(Qt::blue);
              painter.setBrush(Qt::blue);
              painter.drawText(140,300,scoreLabel);
              painter.drawText(230,300,QString::number(nScore));
    painter.setPen(Qt::black);
    painter.setBrush(Qt::green);
    //painter.drawRect(food);
    painter.drawPixmap(food,QPixmap(":/images/Apple.png"));
    painter.drawRects(&vSnakelRect[0],vSnakelRect.size());
    if(blsOver)
        timer->stop();
}
void Snakel::InitSnakel(){
    nDirection=2;
    blsRun=true;
    blsOver=false;
    sDisplay="游戏开始";
    scoreLabel="得分";
    nScore=0;
    food=CreatRect();
   // QRect rect(100,70,10,10);
   // vSnakelRect=rect;
    vSnakelRect.resize(5);
    for(int i=0;i<vSnakelRect.size();i++){
        QRect rect(100,70+10*i,10,10);
        vSnakelRect[vSnakelRect.size()-1-i]=rect;
    }
    SnakelHead=vSnakelRect.first();
    timer=new QTimer(this);
    timer->start(speed);
    connect(timer,SIGNAL(timeout()),SLOT(Snakel_update()));
}
void Snakel::keyPressEvent(QKeyEvent *event){
    QKeyEvent *key=(QKeyEvent*)event;
    switch (key->key()) {
    case Qt::Key_Up:nDirection=1;   //1代表上
        break;
    case Qt::Key_Down:nDirection=2;   //2代表下
        break;
    case Qt::Key_Left:nDirection=3;   //3代表左
        break;
    case Qt::Key_Right:nDirection=4;   //4代表右
        break;
    default:;
    }
}
void Snakel::Snakel_update(){
    sDisplay="";
    SnakelHead=vSnakelRect.first();
    IsEat();
    IsHit();
    for(int j=0;j<vSnakelRect.size()-1;j++){
        vSnakelRect[vSnakelRect.size()-1-j]=vSnakelRect[vSnakelRect.size()-2-j];
    }
    switch(nDirection)
    {
    case 1:
       SnakelHead.setTop(SnakelHead.top()-10);
       SnakelHead.setBottom(SnakelHead.bottom()-10);
        break;
    case 2:
        SnakelHead.setTop(SnakelHead.top()+10);
        SnakelHead.setBottom(SnakelHead.bottom()+10);
        break;
    case 3:
        SnakelHead.setLeft(SnakelHead.left()-10);
        SnakelHead.setRight(SnakelHead.right()-10);
        break;
    case 4:
        SnakelHead.setLeft(SnakelHead.left()+10);
        SnakelHead.setRight(SnakelHead.right()+10);
    default:;
     }
    vSnakelRect[0]=SnakelHead;
    if(SnakelHead.left()<20||SnakelHead.right()>270||SnakelHead.top()<20||SnakelHead.bottom()>270){
        sDisplay="游戏结束";
        blsOver=true;
    }
        update();

    }
QRect Snakel::CreatRect(){
    int x,y;
    x=QRandomGenerator::global()->bounded(0,1000)%25;
    y=QRandomGenerator::global()->bounded(0,1000)%25;
    QRect rect(20+x*10,20+y*10,10,10);
    return rect;
}
void Snakel::IsEat(){
    if(SnakelHead==food){
        SnakelHead=food;
        vSnakelRect.push_back(vSnakelRect.last());
        food=CreatRect();
        nScore+=10;
        if(speed>50){
            speed=speed-10;
            timer->stop();
            timer->start(speed);
        }
    }
}
void Snakel::IsHit(){
    for(int i=1;i<vSnakelRect.size();i++){
        if(SnakelHead==vSnakelRect[i]){
            sDisplay="游戏结束";
            blsOver=true;
            update();
        }
    }
}//游戏结束
